<?php

namespace App\Dao;


use App\Model\EventList;
use Hyperf\Context\ApplicationContext;

class EventDao
{
    private static $instance;

    public static function getInstance()
    {
        if (!self::$instance) {
            self::$instance = Applicationcontext::getcontainer()->get(self::class);
        }
        return self::$instance;
    }


    /**
     * Note - 获取活动消息未读数量
     * DateTime 2024/8/15 11:52
     * User Admin
     * @param $user
     * @return int
     */
    public function notReadNum($user)
    {
        $time = time();
        // $sql = "SELECT count(a.id) as num FROM `".$this->table."` a left join tb_event_read_log r on a.id=r.event_id and r.uid={$user['id']} WHERE ISNULL(r.event_id) and start_time < ? AND end_time > ? and gender={$user['sex']} AND status = ? ";
        // $pd = $this->createCommand($sql);
        // $pd->execute([$time, $time, 1]);
        // $res = $pd->fetch(PDO::FETCH_ASSOC);
        // return $res['num']?:0;
        return EventList::query()->from('tb_event_list AS a')
            ->leftJoin('tb_event_read_log AS r', function ($join) {
                $join->on('a.id', '=', 'r.event_id');
            })->whereNull('r.event_id')
            ->where('r.uid', '=', $user['id'])
            ->where('a.start_time', '<', $time)
            ->where('a.end_time', '>', $time)
            ->where('a.gender', '=', $user['sex'])
            ->where('a.status', '=', 1)
            ->count();
    }

}